# coding=utf-8
from common.cache import redis_cache
from common.utils.decorator import sql_wrapper
from common.utils.db import list_object, get, upsert, delete
from common.bankcard.model import BankCard, BANKCARD_STATUS, BANKCARD_TYPE
from common.utils.alipay_trans_to_bank import BANK_DCT
from common.utils import tz


@sql_wrapper
def get_bankcard(id):
    return get(BankCard, id)


@sql_wrapper
def upsert_bankcard(info, id=None):
    info_bank = info.get('bank', '').strip()
    if not info_bank and id:
        bankcard = get_bankcard(id)
        info_bank = bankcard.bank
    
    info['bank_en'] = ''
    if info_bank in BANK_DCT:
        info['bank_en'] = BANK_DCT.get(info_bank)

    return upsert(BankCard, info, id)


@sql_wrapper
def list_bankcard(query_dct):
    return list_object(query_dct, BankCard)


@sql_wrapper
def delete_bankcard(id):
    delete(BankCard, id)


@sql_wrapper
def get_bankcark_login_info_by_name(name):
    bankcard = BankCard.query.filter(BankCard.name == name).first()
    if bankcard:
        return {
            "id": bankcard.id,
            "code": name,
            "accountName": bankcard.account_holder,
            "accountNumber": bankcard.account_number,
            "loginUsername": bankcard.account,
            "loginPassword": bankcard.password,
            "payPassword": bankcard.pay_password,
            "ukeyPassword": bankcard.ukey_password,
            "status": bankcard.status,
            "balance": bankcard.balance
        }
    else:
        return {}


@sql_wrapper
def get_vaild_bankcard(mch_id, pay_type):
    return BankCard.query.filter(BankCard.status == BANKCARD_STATUS.ENABLE)\
        .filter(BankCard.pay_type == pay_type).filter(BankCard.type == BANKCARD_TYPE.RECHARGE) \
        .filter(BankCard.mch_id == mch_id).all()


@sql_wrapper
def get_bankcard_by_name(name):
    return BankCard.query.filter(BankCard.name == name).first()


@sql_wrapper
def update_bankcard_balance(name, balance):
    bankcard = BankCard.query.filter(BankCard.name == name).first()
    if bankcard is None or bankcard.balance == balance:
        return
    bankcard.balance = balance
    bankcard.save()


@sql_wrapper
def inactive_bankcard_status_by_card_name(card_name):
    bankcard = BankCard.query.filter(BankCard.name == card_name).first()
    if bankcard is None or bankcard.status == BANKCARD_STATUS.DISABLE:
        return
    bankcard.status = BANKCARD_STATUS.DISABLE
    bankcard.save()


@sql_wrapper
def get_bankcard_by_phone(phone, bank):
    if bank != '':
        return BankCard.query.filter(BankCard.phone == phone).filter(BankCard.bank == bank).first()
    else:
        return BankCard.query.filter(BankCard.phone == phone).first()


@sql_wrapper
def get_cards_only():
    cards = BankCard.query.all()
    result = []
    for c in cards:
        data = {}
        data["key"] = c.id
        data["value"] = c.name
        data["type"] = c.type
        result.append(data)
    return result


@sql_wrapper
def get_bankcards():
    return BankCard.query.order_by(BankCard.withdraw_selected_at).all()


@sql_wrapper
def get_bankcard_by_alipay_id(alipay_id):
    return BankCard.query.filter(BankCard.alipay_id == alipay_id).first()


@sql_wrapper
def get_bankcard_name_by_account_number(account_number):
    bankcard = BankCard.query.filter(BankCard.account_number == account_number).first()
    if bankcard:
        return bankcard.name
    else:
        return ''


@sql_wrapper
def get_online_bankcard_by_type(card_type):
    m_bankcard = BankCard.query.filter(BankCard.type == card_type).\
        filter(BankCard.status == BANKCARD_STATUS.ENABLE).all()
    bank_card = []
    for item in m_bankcard:
        data = item.as_dict()
        data['online_status'] = 1 if redis_cache.get_bankcard_online_status(data['name']) else 0
        if item['online_status'] == 1:
            bank_card.append(data)

    return bank_card


@sql_wrapper
def get_bankcard_by_status(status):
    return BankCard.query.filter(BankCard.status == status).all()
